package com.course.controller.admin;

import com.course.dto.PageBean;
import com.course.entity.Result;
import com.course.entity.Teacher;
import com.course.server.TeacherService;
import com.course.vo.SumAndAvg;
import com.course.vo.Courseinfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController("adminTeacherController")
@CrossOrigin
@RequestMapping("/admin/teacher")
@Tag(name="老师接口",description = "老师管理")
public class TeacherController {
    @Resource
    private TeacherService teacherService;

    /**
     * 插入新的教师数据
     * @param teacher
     * @return
     */
    @PostMapping
    public Result addTeacher(@RequestBody Teacher teacher){
        log.info("添加老师:{}",teacher.getTname());
        teacherService.addTeacher(teacher);
        return Result.sucess();
    }

    @GetMapping
    public Result allteacher(){
        log.info("查询全部教师数据");
        List<Teacher> allTeacherList=teacherService.allteacher();
        return Result.sucess(allTeacherList);
    }
    /**
     * 根据教工号查询教工
     * @param tno
     * @return
     */
    @GetMapping("/{tno}")
    public Result searchByTno(@PathVariable String tno){
        log.info("根据教工号查询教师数据:{}",tno);
        List<Teacher> TeacherList=teacherService.searchByTno(tno);
        return Result.sucess(TeacherList);
    }

    /**
     * 根据教工号删除教工
     * @param tno
     * @return
     */
    @DeleteMapping("/{tno}")
    public Result deleteByTno(@PathVariable String tno){
        log.info("根据教工号删除教师数据:{}",tno);
        teacherService.deleteByTno(tno);
        return Result.sucess();
    }

    /**
     * 分页查询教师信息
     * @param page
     * @param pageSize
     * @param tno
     * @param tname
     * @return
     */
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1")Integer page,
                       @RequestParam(defaultValue = "10")Integer pageSize,
                       String tno,String tname){
        log.info("分页条件查询教师信息:{},{},{},{}",page,pageSize,tno,tname);
        PageBean pageBean=teacherService.page(page,pageSize,tno,tname);
        return Result.sucess(pageBean);
    }

    /**
     * 先查询后返回教师相关信息
     * @param teacher
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Teacher teacher){
        log.info("更新教师信息:{}",teacher);
        teacherService.update(teacher);
        return Result.sucess();
    }

    /**
     * 统计不同职称教师数量以及平均工资
     * @return
     */
    @GetMapping("/avg")
    public Result SumAndAvg(){
        log.info("统计不同职称教师数量以及平均工资");
        List<SumAndAvg> sumAndAvg=teacherService.sumAndAvg();
        return Result.sucess(sumAndAvg);
    }

    /**
     * 统计老师所上课程的平均分，最高分，以及最低分
     * @return
     */
    @GetMapping("/courseinfo")
    public Result CourseInfo(){
        log.info("统计老师所上的课平均分，最高分以及最低分");
        List<Courseinfo> courseinfos=teacherService.courseinfo();
        return Result.sucess(courseinfos);
    }
}
